package com.hzqy.commons.utils;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import com.hzqy.commons.log.JLogger;
import com.hzqy.commons.log.LoggerFactory;

public class HttpGetQRCodeUtil {
	
	private static JLogger LOG = LoggerFactory.getLogger(HttpGetQRCodeUtil.class);
	
	private static final String CHARSET = "UTF-8";
	public static final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public static Map<String, String> post(String urlStr, String text) {
		PrintWriter writer = null;
		HttpURLConnection httpConn = null;
		Map<String, String> result = new HashMap<String, String>();
		String msg = "";
		String success = "0";
		
		
		try {// 获取HttpURLConnection连接对象
			URL url = new URL(urlStr);
			httpConn = (HttpURLConnection) url.openConnection();
			// 设置连接属性
			httpConn.setConnectTimeout(2000);
			httpConn.setReadTimeout(3000);
			httpConn.setDoInput(true);
			httpConn.setDoOutput(true);
			httpConn.setUseCaches(false);
			httpConn.setRequestProperty("Connection", "Keep-Alive");
			httpConn.setRequestProperty("Charset", CHARSET);
			httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			httpConn.setRequestMethod("POST");
			
			writer = new PrintWriter(httpConn.getOutputStream());
			writer.write(text);
			writer.flush();

			int respCode = httpConn.getResponseCode();
			if (respCode == 200) {
				success = "1";
				msg = getStringFromInputStream(httpConn.getInputStream());
			}else{
				success = "2";
				msg = "response_status: " + respCode;
			}
		} catch (Exception e) {
			success = "2";
			//msg = e.getMessage();
		}finally {
			if(httpConn != null)
				httpConn.disconnect();
			close(writer);
		}
		
		
		result.put("msg", msg);
		result.put("success", success);
		return result;
	}
	
	public static Map<String, String> get(String urlStr) {
		LOG.debug("二维码urlStr："+urlStr);
		PrintWriter writer = null;
		HttpURLConnection httpConn = null;
		Map<String, String> result = new HashMap<String, String>();
		String msg = "";
		String success = "0";
		
		
		try {// 获取HttpURLConnection连接对象
			URL url = new URL(urlStr);
			httpConn = (HttpURLConnection) url.openConnection();
			// 设置连接属性
			httpConn.setConnectTimeout(2000);
			httpConn.setReadTimeout(3000);
			httpConn.setDoInput(true);
			httpConn.setDoOutput(true);
			httpConn.setUseCaches(false);
			httpConn.setRequestProperty("Connection", "Keep-Alive");
			httpConn.setRequestProperty("Charset", CHARSET);
			httpConn.setRequestProperty("Content-Type", "text/html");
			httpConn.setRequestMethod("GET");
			

			int respCode = httpConn.getResponseCode();
			if (respCode == 200) {
				success = "1";
				msg = getStringFromInputStream(httpConn.getInputStream());
			}else{
				success = "2";
				msg = "response_status: " + respCode;
			}
			LOG.debug("respCode："+respCode);
		} catch (Exception e) {
			success = "2";
			//msg = e.getMessage();
		}finally {
			if(httpConn != null)
				httpConn.disconnect();
			close(writer);
		}
		LOG.debug("二维码获取：success="+success+"=msg="+msg);
		result.put("msg", msg);
		result.put("success", success);
		return result;
	}
	
	
	private static final void close(Closeable closeable) {
		if (closeable != null) {
			try {
				closeable.close();
			} catch (IOException e) {
			}
		}
	}
	
	public static String getStringFromInputStream(InputStream inputStream) {
		StringBuffer str = new StringBuffer();
		BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
		try {
			String temp;
			while ((temp = br.readLine()) != null) {
				str.append(temp);
			}
		} catch (IOException e) {
		} finally {
			close(inputStream);
		}
		return str.toString();
	}
	
	
}
